package ItemBased;


import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.WritableComparable;


public class LongPairWritable implements WritableComparable<LongPairWritable>{

    /**
     * the first field
     */
    LongWritable first;

    /**
     * the second field
     */
    LongWritable second;

    public LongPairWritable() {

    }

    public LongPairWritable(Long t1, Long t2) {
    	first = new LongWritable(t1);
    	second = new LongWritable(t2);
    }
    
    public LongPairWritable(LongWritable t1, LongWritable t2) {
    	first = t1;
    	second = t2;
    }
   
    /**
     * set the first Text
     * @param t1
     */
    public void setFirstText(String t1) {
            first.set(Long.parseLong(t1));
    }
   
    /**
     * set the second text
     * @param t2
     */
    public void setSecondText(String t2) {
            second.set(Long.parseLong(t2));
    }

    /**
     * get the first field
     *
     * @return the first field
     */
    public LongWritable getFirst() {
            return first;
    }

    /**
     * get the second field
     *
     * @return the second field
     */
    public LongWritable getSecond() {
            return second;
    }

    public LongPairWritable get() {
        return this;
    }
    public void write(DataOutput out) throws IOException {
            first.write(out);
            second.write(out);
    }

    public void readFields(DataInput in) throws IOException {
            if (first == null)
                    first = new LongWritable();

            if (second == null)
                    second = new LongWritable();

            first.readFields(in);
            second.readFields(in);
    }

    public int compareTo(LongPairWritable ip2) {
            int cmp = getFirst().compareTo(ip2.getFirst());
            if (cmp != 0) {
                    return cmp;
            }
            return getSecond().compareTo(ip2.getSecond()); // reverse
    }

    public int hashCode() {
            return first.hashCode();
    }

    public boolean equals(Object o) {
            LongPairWritable p = (LongPairWritable) o;
            return first.equals(p.getFirst());
    }
}


